/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea;

import com.googlecode.sca4idea.facet.SCAFacetType;
import com.googlecode.sca4idea.providers.Fabric3SCAProvider;
import com.googlecode.sca4idea.providers.SCAProviderRegistry;
import com.googlecode.sca4idea.providers.VanillaSCAProvider;
import com.intellij.facet.FacetTypeRegistry;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.StdFileTypes;
import org.jetbrains.annotations.NotNull;

/**
 * IDEA Application Component for providing IDE wide configuration.
 *
 * Currently responsible for globally registering XSD resources relevant to composite files.
 *
 * @author paul.jones
 */
public class SCAApplicationComponent implements ApplicationComponent {
    public SCAApplicationComponent() {
    }

    public void initComponent() {
        // Install the SCA facet
        FacetTypeRegistry.getInstance().registerFacetType(SCAFacetType.INSTANCE);

        // Register the composite file types as being XML
        FileTypeManager.getInstance().associateExtension(StdFileTypes.XML, "composite");
        FileTypeManager.getInstance().associateExtension(StdFileTypes.XML, "componentType");

        // Register our static providers
        SCAProviderRegistry providerRegistry = SCAProviderRegistry.getInstance();
        providerRegistry.registerStaticProvider(VanillaSCAProvider.INSTANCE);
        providerRegistry.registerStaticProvider(Fabric3SCAProvider.INSTANCE);

        // Disable since the current XSDs do not work appropriately
//        registerSCAXSD(new String[] {"http://www.osoa.org/xmlns/sca/1.0"}, "sca.xsd");
    }

    public void disposeComponent() {
    }

    @NotNull
    public String getComponentName() {
        return "SCAApplicationComponent";
    }

    private static void registerSCAXSD(String urls[], String xsdName) {
        for (String url : urls) {
            String dtdFile = "/com/googlecode/sca4idea/xsd/" + xsdName;
            ExternalResourceManager.getInstance().addStdResource(url, dtdFile, SCAApplicationComponent.class);
        }
    }
}
